#pragma once 
#include<iostream>
#include<pthread.h>
#include"Mutex.hpp"
namespace CondModule
{
    using namespace MutexModule;
    
    class Cond
    {
    public:
        Cond()
        {
            int n = ::pthread_cond_init(&_cond,nullptr);
            if(n!=0)
            {
                std::cerr << "init error" << std::endl;
                exit(1);
            }
        }
        void Wait(Mutex & mutex)//释放曾经的锁
        {
            int n = ::pthread_cond_wait(&_cond,mutex.LockPtr());
            if(n!=0)
            {
                std::cerr << "wait error" << std::endl;
                exit(5);
            }
        }
        //唤醒一个线程
        void Notify()
        {
            int n = ::pthread_cond_signal(&_cond);
            if(n!=0)
            {
                std::cerr << "signal error" << std::endl;
                exit(3);
            }
        }
        //唤醒多个线程
        void NotifyAll()
        {
            int n = ::pthread_cond_broadcast(&_cond);
            if(n!=0)
            {
                std::cerr << "broadcast error" << std::endl;
                exit(4);
            }
        }
        ~Cond()
        {
            int n = ::pthread_cond_destroy(&_cond);
            if(n!=0)
            {
                std::cerr << "destroy error" << std::endl;
                exit(2);
            }
        }
    private:
    pthread_cond_t _cond;
    };
}